/*
 * @Description:
 * @Author: yangjiecan
 * @Date: 2020-03-13 15:48:05
 * @LastEditTime: 2020-05-09 16:40:46
 * @LastEditors: yangjiecan
 */
 const CryptoJS = require('crypto-js') // 引用AES源码js
 const key = CryptoJS.enc.Utf8.parse('hhhhhhhhhhhhhhhh') // 十六位十六进制数作为密钥
 const iv = CryptoJS.enc.Utf8.parse('1234567887654321') // 十六位十六进制数作为密钥偏移量
  // 解密方法
 function Decrypt (word) {
    // let encryptedHexStr = CryptoJS.enc.Hex.parse('Qntmxrg4cOJXIkIS1WVlAhx7trv88KTrFS2mS9uXjAkX3oHmh0ZHforX8XHpQO9T03BHLvCMzoHaCdOGQcyZsdNVZ8PH22ge5/vCPHQE1KQ=')
   let decrypt = CryptoJS.AES.decrypt(word, key, { iv: iv, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
   let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
   let value = decryptedStr.toString()
   return value
 }
  // 加密方法
 function Encrypt (word) {
   let srcs = CryptoJS.enc.Utf8.parse(JSON.stringify(word))
   let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
   let hexStr = encrypted.ciphertext.toString().toUpperCase()
   let oldHexStr = CryptoJS.enc.Hex.parse(hexStr)
    // 将密文转为Base64的字符串
   let base64Str = CryptoJS.enc.Base64.stringify(oldHexStr)
  // 这里根据需求返回 base64Str 或 hexStr(解密时有小小差别)
   return base64Str
 }
 export default {
   Decrypt,
   Encrypt
 }
